# coding=utf-8
'''
  自动获取 i问财网站 指定请求数据获取最新消息
    http://www.iwencai.com/stockpick/search?ts=1&f=1&qs=stockhome_topbar_click&w=2021年6月9号 5日均线上穿60日均线 diff线上穿dea线 非新股 非st 非*st
'''

from urllib.parse import quote
import time
import requests


def get_data_encode():
    d = time.localtime(time.time())
    yesterday = "{}年{}月{}日".format(d.tm_year, d.tm_mon, d.tm_mday - 1)
    data = '{} 5日均线上穿60日均线 diff线上穿dea线 非新股 非st 非*st'.format(yesterday)
    print(data)
    return quote(data)


def get_html():
    # url = 'www.iwencai.com/stockpick/search?ts=1&f=1&qs=stockhome_topbar_click&w=2021年6月22日5日均线上穿60日均线'
    url = 'http://www.iwencai.com/stockpick/search?ts=1&f=1&qs=stockhome_topbar_click&w='
    url += get_data_encode()
    print(url)
    headers = {
        'User-Agent':
        'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36',
        'Cookie':
        'cid=9c16d61bad9da4ef98182144803828b41605581265; ComputerID=9c16d61bad9da4ef98182144803828b41605581265; WafStatus=0; other_uid=Ths_iwencai_Xuangu_dwdwnnd9n3sdq9hi6vr3j99yfsdohmqp; PHPSESSID=fd2a2b194ba86daf91a129e3c678c371; guideState=1; v=A5lc65TAHdp0gsFOsca4MrE5oY5wJoz2dxuxb7tOFuY_7Lf4A3adqAdqwTVI',
        'Referer': url
    }
    conn = requests.get(url, headers=headers)
    print(conn.status_code)

    if conn.status_code == 200:
        with open('data.html', 'w') as f:
            f.write(conn.text)


get_html()